﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using org.se.fs.presenter.Library;

namespace org.se.fs.presenter.Domain
{
    public class RegularLoan : IRegularLoan, INotifyPropertyChanged, IValidatable
    {
        private string _code;
        private string _description;
        private decimal _regularAmount;
        private decimal _regularInterest;
        private decimal _extensionInterest;
        private decimal _principalAmount;
        private decimal _serviveFee;
        private bool _reLoan;
        public event PropertyChangedEventHandler PropertyChanged;
        public IList<DomainValidator> ErrorDomain;

        public RegularLoan()
        {
            ErrorDomain = new List<DomainValidator>();
        }

        public ulong Id
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        public string Code
        {
            get { return _code; }
            set
            {
                _code = value;
                OnPropertyChanged("Code");
            }
        }

        public string Description
        {
            get { return _description; }
            set { 
                _description = value;
                OnPropertyChanged("Description");
            }
        }

        public bool IsPosted
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        public bool IsReLoan
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        public decimal RegularAmount
        {
            get { return _regularAmount; }
            set
            {
                _regularAmount = value;
                OnPropertyChanged("RegularAmount");
            }
        }

        public decimal RegularInterest
        {
            get { return _regularInterest; }
            set
            {
                _regularInterest = value;
                OnPropertyChanged("RegularInterest");
            }
        }

        public decimal ExtensionInterest
        {
            get { return _extensionInterest; }
            set
            {
                _extensionInterest = value;
                OnPropertyChanged("ExtensionInterest");
            }
        }

        public decimal PrincipalAmount
        {
            get { return _principalAmount; }
            set
            {
                _principalAmount = value;
                OnPropertyChanged("PrincipalAmount");
            }
        }

        public decimal ServiveFee
        {
            get { return _serviveFee; }
            set
            {
                _serviveFee = value;
                OnPropertyChanged("ServiveFee");
            }
        }

        public DateTime CreatedDate
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        public bool ReLoan
        {
            get { return _reLoan; }
            set
            {
                _reLoan = value;
                OnPropertyChanged("ReLoan");
            }
        }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #region IValidatable Members
        public bool Validate()
        {
            ErrorDomain = new List<DomainValidator>();

            if (String.IsNullOrEmpty(Code))
                ErrorDomain.Add(new DomainValidator("Code", "Code must not be null."));

            if (String.IsNullOrEmpty(Description))
                ErrorDomain.Add(new DomainValidator("Description", "Description must not be null."));

            if (RegularAmount < 0)
                ErrorDomain.Add(new DomainValidator("RegularAmount", "Regular amount must be greater than or equal to zero."));

            if (RegularInterest < 0)
                ErrorDomain.Add(new DomainValidator("RegularInterest", "Regular interest must be greater than or equal to zero."));

            if (ExtensionInterest < 0)
                ErrorDomain.Add(new DomainValidator("ExtensionInterest", "Extension interest must be greater than or equal to zero."));

            if (PrincipalAmount < 0)
                ErrorDomain.Add(new DomainValidator("PrincipalAmount", "Principal amount must be greater than or equal to zero."));

            if (ServiveFee < 0)
                ErrorDomain.Add(new DomainValidator("ServiveFee", "Servive fee must be greater than or equal to zero."));

            return ErrorDomain.Count == 0; //-- no error
        }
        #endregion
    }
}
